<?php
namespace app\admin\controller;

use think\facade\View;

use app\admin\model\TopicCategory as CategoryModel;
use app\admin\model\TopicContent as ContentModel;

/**
 * 专题管理  
 * @author huwhis@163.com
 * @version   0.0.1
 */
class TopicContent extends Base
{
    public function index($cid = 0)
    {
        $categoryTree = obj_tree(CategoryModel::field('id, pid, name, type')->select());

        $categories = CategoryModel::column('name', 'id');

        $where = [];
        
        if ($cid) {
            $where['cid'] = $cid;
        }
        
        $model = new ContentModel();

        $data = $model->where($where)->field('id,cid,title,topic_url,status,top,sort,username,clicks,create_time')->order('id', 'desc')->paginate();

        return View::fetch('index', [
            'categoryTree' => $categoryTree,
            'categories' => $categories,
            'data' => $data,
            'cid' => $cid,
            'status' => [1=>'正常',2=>'下架'],
            'top' => [1=>'非推荐',2=>'推荐']
        ]);
    }

    public function info($cid = 0, $id = 0)
    {
        if (!$cid) {
            $this->error('请选择栏目');
        }
        if ($id) {
            $data = ContentModel::find($id);
        } else {
            $data = [
                'id' => 0,
                'title' => '',
                'writer' => '',
                'source' => '',
                'title_pic' => '',
                'keywords' => '',
                'summary' => '',
                'topic_url' => '',
                'top' => 0,
                'sort' => 0,
                'clicks' => 0,
                'type' => 0,
                'topic_path'=> '',
                'topic_content'=> ''
            ];
        }

        $category_info = CategoryModel::field('id, pid, name')->find($cid);

        return View::fetch('save', [
            'cid' => $cid,
            'category_info' => $category_info,
            'data' => $data,
        ]);
    }

    public function checkUrl($topic_url = "")
    {
        if(file_exists(app()->getRootPath() . 'public'.$topic_url)){
            return ['code' => 0,'msg'=>"目录存在, 请注意"];
        } else {
            return ['code' => 1,'msg'=>"目录可用"];
        }
    }

    public function save()
    {
        if ($this->app->request->isPost()) {
            $param = $this->app->request->param();
            // var_dump($param);
            // exit;
            if (!$param['cid']) {
                $this->error('请选择栏目');
            }
            if ($param['title'] == '') {
                $this->error("标题不能为空");
            }

            if (((int) $param['type'] == 0 || (int) $param['type'] == 1) && $param['topic_url'] == '') {
                $this->error("专题路径不能为空");
            }

            $model = new ContentModel();

            try {
                if ($param['id'] != 0) {
                   $model->update($param);
                } else {
                    $param['userid'] = $this->uid;
                    $param['username'] = $this->username;

                    unset($param['id']);
                    $model->create($param);
                }
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                $this->error("错误提示:".$msg);
            }
            $this->success('操作成功', url('topic_content/index', ['cid'=>$param['cid']]));
        }
    }

    public function delete($id = null)
    {
        if ($this->app->request->isAjax()) {
            $sun = \app\admin\model\TopicSun::where('tid', $id)->select();

            if (!$sun->isEmpty()) {
                return ['code' => 0,'msg'=>"删除失败, 子专题不为空"];
            }

            $info = \app\admin\model\topicInfo::where('tid', $id)->select();

            if (!$info->isEmpty()) {
                return ['code' => 0,'msg'=>"删除失败, 专题信息不为空"];
            }

            if (ContentModel::destroy($id)) {
                return ['code' => 1,'msg'=>"删除成功"];
            } else {
                return ['code' => 0,'msg'=>"删除失败"];
            }
        }
    }

    public function status($id)
    {
        $content = ContentModel::find($id);
        
        $content->status = 3 - $content->status;

        if ($content->save()) {
            return ['code' => 1,'msg'=>"成功", "status"=>$content->status];
        } else {
            return ['code' => 0,'msg'=>"失败"];
        }
    }

    public function dotop($id)
    {
        $content = ContentModel::find($id);
        
        $content->top = 3 - $content->top;

        if ($content->save()) {
            return ['code' => 1,'msg'=>"成功", "top"=>$content->top];
        } else {
            return ['code' => 0,'msg'=>"失败"];
        }
    }

    public function tlist($tcid)
    {
        if ($this->app->request->isPost()) {
            $where = [];

            if ($tcid) {
                $where[] = ['cid', '=', $tcid];
            }

            $list = ContentModel::where($where)->field('id, cid, title')->order('id desc')->select();

            return ['code'=>1, 'list'=>$list];
        }
    }    
}
